<template>
  <el-card shadow="never">
    <el-tree
      :data="data"
      show-checkbox
      node-key="id"
      :default-expanded-keys="[2, 3]"
      :default-checked-keys="[5]"
      :props="defaultProps">
    </el-tree>
  </el-card>
</template>

<script>
import { getModuleInfs } from '../../../api/code'

export default {
  name: 'module-inf-tree',
  data () {
    return {
      data: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getList () {
      getModuleInfs().then(res => {
        const data = res.data
        this.data = this.parseTree(data, 'moduleId', 'moduleName', 'tables', 'tableId', 'tableName')
      })
    },

    parseTree (list, id, label, children, childrenId, childrenLabel) {
      const treeList = []
      for (let i = 0; i < list.length; i++) {
        const item = list[i]
        const treeChildrenList = []
        if (item[children]) {
          const childrenList = item[children]
          for (let j = 0; j < childrenList.length; j++) {
            const childrenItem = childrenList[j]
            treeChildrenList.push({
              id: childrenItem[childrenId],
              label: childrenItem[childrenLabel]
            })
          }
        }
        treeList.push({
          id: item[id],
          label: item[label],
          children: treeChildrenList
        })
      }
      return treeList
    }
  }
}
</script>

<style scoped>

</style>
